home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / Array.cc < prev    next >
C/C++ Source or Header  |  1996-11-07  |  4KB  |  194 lines

  1. // Template array classes
  2. /*
  3.  
  4. Copyright (C) 1996 John W. Eaton
  5.  
  6. This file is part of Octave.
  7.  
  8. Octave is free software; you can redistribute it and/or modify it
  9. under the terms of the GNU General Public License as published by the
  10. Free Software Foundation; either version 2, or (at your option) any
  11. later version.
  12.  
  13. Octave is distributed in the hope that it will be useful, but WITHOUT
  14. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with Octave; see the file COPYING.  If not, write to the Free
  20. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22. */
  23.  
  24. #if defined (__GNUG__)
  25. #pragma implementation
  26. #endif
  27.  
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31.  
  32. #include <cassert>
  33.  
  34. #include <iostream.h>
  35.  
  36. #include "Array.h"
  37.  
  38. #if defined (HEAVYWEIGHT_INDEXING)
  39. #include "idx-vector.h"
  40. #include "Array-idx.h"
  41. #endif
  42.  
  43. #include "lo-error.h"
  44.  
  45. // One dimensional array class.  Handles the reference counting for
  46. // all the derived classes.
  47.  
  48. template <class T>
  49. Array<T>::Array (int n, const T& val)
  50. {
  51.   rep = new ArrayRep (n);
  52.  
  53.   for (int i = 0; i < n; i++)
  54.     rep->data[i] = val;
  55.  
  56. #ifdef HEAVYWEIGHT_INDEXING
  57.   max_indices = 1;
  58.   idx_count = 0;
  59.   idx = 0;
  60. #endif
  61. }
  62.  
  63. template <class T>
  64. Array<T>::~Array (void)
  65. {
  66.   if (--rep->count <= 0)
  67.     delete rep;
  68.  
  69. #ifdef HEAVYWEIGHT_INDEXING
  70.   delete [] idx;
  71. #endif
  72. }
  73.  
  74. template <class T>
  75. Array<T>&
  76. Array<T>::operator = (const Array<T>& a)
  77. {
  78.   if (this != &a && rep != a.rep)
  79.     {
  80.       if (--rep->count <= 0)
  81.     delete rep;
  82.  
  83.       rep = a.rep;
  84.       rep->count++;
  85.     }
  86.  
  87. #ifdef HEAVYWEIGHT_INDEXING
  88.   max_indices = 1;
  89.   idx_count = 0;
  90.   idx = 0;
  91. #endif
  92.  
  93.   return *this;
  94. }
  95.  
  96. template <class T>
  97. void
  98. Array<T>::resize (int n)
  99. {
  100.   if (n < 0)
  101.     {
  102.       (*current_liboctave_error_handler) ("can't resize to negative dimension");
  103.       return;
  104.     }
  105.  
  106.   if (n == length ())
  107.     return;
  108.  
  109.   ArrayRep *old_rep = rep;
  110.   const T *old_data = data ();
  111.   int old_len = length ();
  112.  
  113.   rep = new ArrayRep (n);
  114.  
  115.   if (old_data && old_len > 0)
  116.     {
  117.       int min_len = old_len < n ? old_len : n;
  118.  
  119.       for (int i = 0; i < min_len; i++)
  120.     xelem (i) = old_data[i];
  121.     }
  122.  
  123.   if (--old_rep->count <= 0)
  124.     delete old_rep;
  125. }
  126.  
  127. template <class T>
  128. void
  129. Array<T>::resize (int n, const T& val)
  130. {
  131.   if (n < 0)
  132.     {
  133.       (*current_liboctave_error_handler) ("can't resize to negative dimension");
  134.       return;
  135.     }
  136.  
  137.   if (n == length ())
  138.     return;
  139.  
  140.   ArrayRep *old_rep = rep;
  141.   const T *old_data = data ();
  142.   int old_len = length ();
  143.  
  144.   rep = new ArrayRep (n);
  145.  
  146.   int min_len = old_len < n ? old_len : n;
  147.  
  148.   if (old_data && old_len > 0)
  149.     {
  150.       for (int i = 0; i < min_len; i++)
  151.     xelem (i) = old_data[i];
  152.     }
  153.  
  154.   for (int i = old_len; i < n; i++)
  155.     xelem (i) = val;
  156.  
  157.   if (--old_rep->count <= 0)
  158.     delete old_rep;
  159. }
  160.  
  161. template <class T>
  162. T *
  163. Array<T>::fortran_vec (void)
  164. {
  165.   if (rep->count > 1)
  166.     {
  167.       --rep->count;
  168.       rep = new ArrayRep (*rep);
  169.     }
  170.   return rep->data;
  171. }
  172. template <class T>
  173. T
  174. Array<T>::range_error (const char *fcn, int n) const
  175. {
  176.   (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n);
  177.   return T ();
  178. }
  179.  
  180. template <class T>
  181. T&
  182. Array<T>::range_error (const char *fcn, int n)
  183. {
  184.   (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n);
  185.   static T foo;
  186.   return foo;
  187. }
  188.  
  189. /*
  190. ;;; Local Variables: ***
  191. ;;; mode: C++ ***
  192. ;;; End: ***
  193. */
  194.